const { updateBasicInformation } = require('../../../../api/index');
const { myToast } = require('../../../../utils/util');
const { getBatchDict } = require('../../../../utils/dict');

// packageA/pages/authentication/otherForm/index.js
Page({
  data: {
    educationDicts: [],
    formData: {
      id: "",
      avatar: "",
      phone: "",
      highestEducation: "",
      workUnit: "",
      strongPoint: "",
      interest: "",
      otherRequirement: ""
    }
  },
  //  修改表单数据
  onFormData(event) {
    const { prop } = event.currentTarget.dataset;
    this.data.formData[prop] = event.detail;
  },
  async onSubmit( event ) {
    fetchUpdateBasicInformation.call(this);
  },
  async onLoad(options) {
    await fetchDists.call(this);
    await getResult.call(this);
    // fetchUpdateBasicInformation.call(this);
  }
})

async function fetchUpdateBasicInformation() {
  const { errors = [] } = await updateBasicInformation( this.data.formData );
  if (errors.length > 0) {
    myToast('error', errors[0].message);
    return;
  }

  //  成功 直接跳到 成功页面 - 接着在从成功页面跳转回对应的页面
  //  提交成功 跳转到 成功页面
  const url = '/pages/home/index';
  wx.navigateTo({ url: `/packageA/pages/success/index?type=base_message&url=${ url }` });
}

//  处理 formData 

//  处理回显数据
function getResult() {
  const base = wx.getStorageSync('studentMsg');
  base.highestEducationName = this.data.educationDicts.find(_s => _s.code === base.highestEducation)?.name;
  this.setData({ formData: base });
}


//  获取字典项数据
async function fetchDists() {
  const [ educationDicts ] = await getBatchDict(['EDUCATION']);
  this.data.educationDicts = educationDicts;
}